<?php

if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Settings extends AUTH_Controller {

  var $enable_face_detection = FALSE;

  public function index() {
    redirect('settings/profile');
  }

  public function password() {
    $change_password_status = NULL;
    if ($this->is_post()) {
      $change_password_status = 3;
      $current_password = $this->sanitize($this->input->post('current_password', TRUE));
      $new_password = $this->sanitize($this->input->post('new_password', TRUE));
      $confirm_new_password = $this->sanitize($this->input->post('confirm_new_password', TRUE));
      if (!empty($current_password) && !empty($new_password) && !empty($confirm_new_password) && ($new_password == $confirm_new_password)) {
        $change_password_status = 2;
        if (md5($current_password) == $this->current_user['user_password']) {
          $update_password = $this->user_model->update_password($this->current_user['user_id'], md5($new_password));
          $change_password_status = 3;
          if ($update_password['status'] == 1 && !empty($update_password['result'])) {
            $change_password_status = 1;
          }
        }
      }
      $this->session->set_flashdata('change_password_status', $change_password_status);
      redirect('settings/password');
      return;
    }
    $view_data = array();
    $view_data['menu_active'] = 'settings';
    $view_data['change_password_status'] = $change_password_status;
    $this->load_view('settings_password', $view_data);
  }

  public function profile() {
    $change_profile_status = NULL;
    $positions_array = array(
        'Senior Project Manager', 'Project Manager',
        'Senior Team Lead', 'Senior Technical Lead',
        'Product Manager', 'Software Architect', 'Project Lead',
        'Human Resource Manager', 'Human Resource Administrator', 'Human Resource Assistant',
        'Senior Software Engineer', 'Software Engineer', 'Junior Software Engineer', 'Software Engineer Trainee',
        'Senior Network Engineer', 'Network Engineer', 'Junior Network Engineer', 'Network Engineer Trainee',
        'Senior Test Engineer', 'Test Engineer', 'Junior Test Engineer', 'Test Engineer Trainee',
        'Senior Graphic Designer', 'Graphic Designer', 'Junior Graphic Designer', 'Graphic Designer Trainee',
    );
    $exp_array = array('6 months', '1 Year');
    for ($i = 2; $i <= 25; $i++) {
      $exp_array[] = $i . ' Years';
    }
    if ($this->is_post()) {
      $change_profile_status = 2;
      $user_about_me = $this->sanitize($this->input->post('user_about_me', TRUE));
      $user_position = $this->sanitize($this->input->post('user_position', TRUE));
      $user_experience = $this->sanitize($this->input->post('user_experience', TRUE));
      $user_skills = $this->sanitize($this->input->post('user_skills', TRUE));
      $user_join_date = $this->sanitize($this->input->post('user_join_date', TRUE));
      $user_display_name = $this->sanitize($this->input->post('user_display_name', TRUE));
      $user_gender = $this->sanitize($this->input->post('user_gender', TRUE));
      $user_hobbies = $this->sanitize($this->input->post('user_hobbies', TRUE));
      $user_place = $this->sanitize($this->input->post('user_place', TRUE));
      $user_birthday_date = $this->sanitize($this->input->post('user_birthday_date', TRUE));
      $user_home_email = $this->sanitize(strtolower($this->input->post('user_home_email', TRUE)));
      $user_contact_no = $this->sanitize($this->input->post('user_contact_no', TRUE));
      $user_notification = $this->sanitize($this->input->post('user_notification', TRUE));
      $user_notification = ($user_notification == 1) ? $user_notification : 0;
      $user_update_data = array();
      $user_update_data['user_about_me'] = $user_about_me;
      $user_update_data['user_notification'] = $user_notification;
      if (in_array($user_position, $positions_array)) {
        $user_update_data['user_position'] = $user_position;
      }
      if (in_array($user_experience, $exp_array)) {
        $user_update_data['user_experience'] = $user_experience;
      }
      $user_update_data['user_skills'] = ucwords($user_skills);
      if (strtotime($user_join_date)) {
        $user_update_data['user_join_date'] = @date('Y-m-d H:i:s', strtotime($user_join_date));
      }
      if (preg_match('/^[a-zA-Z .]*$/', $user_display_name) && (strlen($user_display_name) > 2)) {
        if (!empty($user_display_name)) {
          $user_update_data['user_display_name'] = ucwords(substr($user_display_name, 0, 30));
        } else {
          $user_update_data['user_display_name'] = ucwords($this->current_user['user_name']);
        }
      } else {
        $user_update_data['user_display_name'] = ucwords($this->current_user['user_name']);
      }
      if ($user_gender == 'Male' || $user_gender == 'Female') {
        $user_update_data['user_gender'] = $user_gender;
      }
      $user_update_data['user_hobbies'] = ucwords($user_hobbies);
      $user_update_data['user_place'] = ucwords($user_place);
      if (strtotime($user_birthday_date)) {
        $user_update_data['user_birthday_date'] = @date('Y-m-d H:i:s', strtotime($user_birthday_date));
      }
      if (filter_var($user_home_email, FILTER_VALIDATE_EMAIL)) {
        $user_update_data['user_home_email'] = $user_home_email;
      }
      if (is_numeric(substr($user_contact_no, 1)) && strlen($user_contact_no) >= 10) {
        if (strlen($user_contact_no) == 10) {
          $user_contact_no = '+91' . $user_contact_no;
        }
        $user_update_data['user_contact_no'] = substr($user_contact_no, 0, 13);
      }
      $update_profile = $this->user_model->update_profile($this->current_user['user_id'], $user_update_data);
      if ($update_profile['status'] == 1 && !empty($update_profile['result'])) {
        $change_profile_status = 1;
      }
      $this->session->set_flashdata('change_profile_status', $change_profile_status);
      redirect('settings/profile');
      return;
    }
    $view_data = array();
    $view_data['menu_active'] = 'settings';
    $view_data['user_data'] = $this->current_user;
    $view_data['positions_array'] = $positions_array;
    $view_data['exp_array'] = $exp_array;
    $this->load_view('settings_profile', $view_data);
    return;
  }

  public function photo($option = NULL) {
    if ($option == 'remove') {
      $current_image = 'assets/photos/' . $this->current_user['user_image'];
      $default_image = '3c58e494edfe0fdcbfc9ba92381cbfa6.jpg';
      $user_update_data['user_image'] = $default_image;
      $this->user_model->update_profile($this->current_user['user_id'], $user_update_data);
      if (file_exists($current_image) && ($this->current_user['user_image'] != $default_image)) {
        @unlink($current_image);
      }
      $this->session->set_flashdata('change_photo_status', 3);
      redirect('settings/photo');
      return;
    }
    if ($this->is_post()) {
      $err_msg = NULL;
      $default_image = '3c58e494edfe0fdcbfc9ba92381cbfa6.jpg';
      $file_name = md5(rand(1111111111, 999999999)) . '.jpg';
      $new_image_file = 'assets/photos/' . $file_name;
      $config['upload_path'] = 'assets/temp/';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_size'] = '10240';
      $this->load->library('upload', $config);
      $image_upload = $this->upload->do_upload('user_image_file');
      $err_msg = $this->upload->display_errors();
      if ($image_upload) {
        $uploaded_image = $this->upload->data();
        $image_full_path = $uploaded_image['full_path'];
        $imgae_file_type = $uploaded_image['file_type'];

        // Convering & Creating JPG Image
        switch ($imgae_file_type) {
          case "image/gif":
            $img_create = imageCreateFromGIF($image_full_path);
            break;
          case "image/jpeg":
            $img_create = imageCreateFromJPEG($image_full_path);
            break;
          case "image/png":
            $img_create = imageCreateFromPNG($image_full_path);
            break;
        }

        if ($this->enable_face_detection) {
          $face_data_arr = array();
          $this->load->library('face_detector/face_detector');
          $this->face_detector->face_detect($img_create);
          $face_data = $this->face_detector->toJson();
          if (!empty($face_data)) {
            $face_data_arr = @json_decode($face_data, TRUE);
          }
        }

        $orig_width = ImageSX($img_create);
        $orig_height = ImageSY($img_create);
        if ($orig_width > $orig_height) {
          $new_width = $orig_height;
          $new_height = $orig_height;
          $x = @round(($orig_width / 2) - ($new_width / 2));
          $y = 0;
        } elseif ($orig_height > $orig_width) {
          $new_width = $orig_width;
          $new_height = $orig_width;
          $x = 0;
          $y = @round(($orig_height / 2) - ($new_height / 2));
        } else {
          $new_width = $orig_width;
          $new_height = $orig_height;
          $x = 0;
          $y = 0;
        }

        $dst = ImageCreateTrueColor($new_height, $new_width);
        $white = ImageColorAllocate($dst, 255, 255, 255);
        ImageFill($dst, 0, 0, $white);
        imagecopyresampled($dst, $img_create, 0, 0, $x, $y, $new_width, $new_height, $new_width, $new_height);
        imageinterlace($dst, true);
        ImageJpeg($dst, $new_image_file, 100);

        // Image resizing
        $config['image_library'] = 'gd2';
        $config['source_image'] = $new_image_file;
        $config['new_image'] = $new_image_file;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = 250;
        $config['height'] = 250;
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();

        // Removing temp file
        @unlink($image_full_path);

        if (file_exists($new_image_file)) {

          // Removing existing photo
          $current_image = 'assets/photos/' . $this->current_user['user_image'];
          if (file_exists($current_image) && ($this->current_user['user_image'] != $default_image)) {
            @unlink($current_image);
          }

          // Updating new photo
          $user_update_data['user_image'] = $file_name;
          $this->user_model->update_profile($this->current_user['user_id'], $user_update_data);
          $this->session->set_flashdata('change_photo_status', 1);
          redirect('settings/photo');
          return;
        }
        $err_msg = 'Error occured. Try again !';
      }
      $this->session->set_flashdata('change_photo_status', 2);
      $this->session->set_flashdata('change_photo_error', $err_msg);
      redirect('settings/photo');
      return;
    }
    $view_data = array();
    $view_data['menu_active'] = 'settings';
    $view_data['user_data'] = $this->current_user;
    $this->load_view('settings_photo', $view_data);
  }

  public function others() {
    $change_settings_status = NULL;
    if ($this->is_post()) {
      $change_settings_status = 2;
      $user_notification = $this->sanitize($this->input->post('user_notification', TRUE));
      $user_notification = ($user_notification == 1) ? $user_notification : 0;
      $user_online_mode = $this->sanitize($this->input->post('user_online_mode', TRUE));
      $user_online_mode = ($user_online_mode == 1) ? $user_online_mode : 0;
      $user_update_data = array();
      $user_update_data['user_notification'] = $user_notification;
      //$user_update_data['user_online_mode'] = $user_online_mode;
      $update_profile = $this->user_model->update_profile($this->current_user['user_id'], $user_update_data);
      if ($update_profile['status'] == 1 && !empty($update_profile['result'])) {
        $change_settings_status = 1;
      }
      $this->session->set_flashdata('change_settings_status', $change_settings_status);
      redirect('settings/others');
      return;
    }
    $api_apps = array();
    $get_my_apps = $this->api_model->get_my_apps($this->current_user['user_id']);
    if ($get_my_apps['status'] == 1 && !empty($get_my_apps['result'])) {
      $api_apps = $get_my_apps['result'];
    }
    $view_data = array();
    $view_data['menu_active'] = 'settings';
    $view_data['user_data'] = $this->current_user;
    $view_data['api_apps'] = $api_apps;
    $this->load_view('settings_others', $view_data);
    return;
  }

  public function un_authorize_app($app_id) {
    $change_settings_status = 2;
    if (!empty($app_id) && is_numeric($app_id)) {
      $un_authorize_app = $this->api_model->un_authorize_app($app_id, $this->current_user['user_id']);
      if ($un_authorize_app['status'] == 1 && !empty($un_authorize_app['result'])) {
        $change_settings_status = 1;
      }
    }
    $this->session->set_flashdata('change_settings_status', $change_settings_status);
    redirect('settings/others');
    return;
  }

}

?>
